package com.gisocn.onemap.presenter;

import android.util.Log;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.MapGeometry;
import com.gisocn.onemap.base.BasePresenter;
import com.gisocn.onemap.base.Constant;
import com.gisocn.onemap.interfaces.DownLoadListener;
import com.gisocn.onemap.model.GetDirectoryData;
import com.gisocn.onemap.net.ApiCallback;
import com.gisocn.onemap.utils.ExecutorsManager;
import com.gisocn.onemap.utils.L;
import com.gisocn.onemap.utils.Utils;
import com.gisocn.onemap.view.CollectionView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import jsqlite.Callback;
import jsqlite.Database;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;

/* loaded from: classes2.dex */
public class CollectionPresent extends BasePresenter<CollectionView> {
    HttpURLConnection conn;
    Geometry geometry;
    String savaFileNamePath;
    private URL url = null;

    public CollectionPresent(CollectionView collectionView) {
        attachView(collectionView);
    }

    public void getGeometry(String str, int i) {
        this.geometry = null;
        String str2 = Constant.SAVE_PATH_SHP + str;
        if (str.contains(".")) {
            str = str.substring(0, str.lastIndexOf("."));
        } else {
            str2 = Constant.SAVE_PATH_SHP + str + ".db";
        }
        try {
            String file = new File(str2).toString();
            Class.forName("jsqlite.JDBCDriver").newInstance();
            Database database = new Database();
            database.open(file, 1);
            Callback callback = new Callback() { // from class: com.gisocn.onemap.presenter.CollectionPresent.1
                @Override // jsqlite.Callback
                public void columns(String[] strArr) {
                }

                @Override // jsqlite.Callback
                public void types(String[] strArr) {
                }

                @Override // jsqlite.Callback
                public boolean newrow(String[] strArr) {
                    JsonParser jsonParser;
                    try {
                        jsonParser = new JsonFactory().createJsonParser(Utils.getMULTIPOLYGONWktToJson(strArr[0], 4326));
                    } catch (IOException e) {
                        e.printStackTrace();
                        jsonParser = null;
                    }
                    MapGeometry jsonToGeometry = GeometryEngine.jsonToGeometry(jsonParser);
                    CollectionPresent.this.geometry = jsonToGeometry.getGeometry();
                    return false;
                }
            };
            String str3 = "SElECT AsText(Geometry) from " + str + " where pk_uid=" + i;
            Log.e("sql", str3);
            database.exec(str3, callback);
            database.close();
            Log.e("query", "查询结束");
            ((CollectionView) this.mvpView).getGeometry(this.geometry);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e2) {
            e2.printStackTrace();
        } catch (InstantiationException e3) {
            e3.printStackTrace();
        } catch (Exception e4) {
            e4.printStackTrace();
        }
    }

    public void getDownLoading(final String str, final String str2, final String str3, final boolean z, final int i, final DownLoadListener downLoadListener) {
        L.e("getDownLoading_fileUrl" + str);
        ExecutorsManager.getInstance().execute(new Runnable() { // from class: com.gisocn.onemap.presenter.CollectionPresent.2
            @Override // java.lang.Runnable
            public void run() {
                String str4;
                try {
                    try {
                        str4 = new URI(str).toASCIIString();
                    } catch (URISyntaxException e) {
                        e.printStackTrace();
                        str4 = "";
                    }
                    CollectionPresent.this.url = new URL(str4);
                    Log.e("ASCIIUrl", CollectionPresent.this.url.getPath());
                    try {
                        CollectionPresent collectionPresent = CollectionPresent.this;
                        collectionPresent.conn = (HttpURLConnection) collectionPresent.url.openConnection();
                        File file = new File(Constant.PLAN_DATA_BASE_PATH + str2);
                        if (!file.exists()) {
                            file.mkdirs();
                        }
                        CollectionPresent.this.savaFileNamePath = Constant.PLAN_DATA_BASE_PATH + str2 + str3;
                        File file2 = new File(CollectionPresent.this.savaFileNamePath);
                        if (!file2.exists()) {
                            file2.createNewFile();
                        }
                        Log.e("downLoadFile", "downLoadFile");
                        downLoadListener.onStart();
                        try {
                            InputStream inputStream = CollectionPresent.this.conn.getInputStream();
                            long contentLength = CollectionPresent.this.conn.getContentLength();
                            long j = 0;
                            if (contentLength != 0 && contentLength != -1) {
                                FileOutputStream fileOutputStream = new FileOutputStream(file2);
                                byte[] bArr = new byte[1024];
                                while (true) {
                                    int read = inputStream.read(bArr);
                                    if (read != -1) {
                                        fileOutputStream.write(bArr, 0, read);
                                        j += read;
                                        long j2 = 100 * j;
                                        downLoadListener.onProgress((int) (j2 / contentLength));
                                        if (((int) (j2 / contentLength)) == 100) {
                                            downLoadListener.onFinish(CollectionPresent.this.savaFileNamePath);
                                            if (z) {
                                                ((CollectionView) CollectionPresent.this.mvpView).getDownLoadSuccess(CollectionPresent.this.savaFileNamePath, i);
                                            } else {
                                                ((CollectionView) CollectionPresent.this.mvpView).getDownLoadSuccess(CollectionPresent.this.savaFileNamePath);
                                            }
                                        }
                                    } else {
                                        inputStream.close();
                                        fileOutputStream.close();
                                        return;
                                    }
                                }
                            }
                            Log.e("totalLength", "" + contentLength);
                            downLoadListener.onFailure();
                        } catch (FileNotFoundException e2) {
                            e2.printStackTrace();
                        } catch (IOException e3) {
                            e3.printStackTrace();
                        }
                    } catch (IOException e4) {
                        e4.printStackTrace();
                    }
                } catch (MalformedURLException e5) {
                    e5.printStackTrace();
                }
            }
        });
    }

    public void getDirectoryData(Map<String, String> map) {
        addSubscription(this.apiService.getDirectoryData(map), new ApiCallback<GetDirectoryData>() { // from class: com.gisocn.onemap.presenter.CollectionPresent.3
            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFinish() {
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onSuccess(GetDirectoryData getDirectoryData) {
                if (getDirectoryData.getReturnCode() == 0) {
                    ((CollectionView) CollectionPresent.this.mvpView).getDirectoryDataSuccess(getDirectoryData);
                } else {
                    ((CollectionView) CollectionPresent.this.mvpView).fail(getDirectoryData.getMessage());
                }
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFailure(String str) {
                ((CollectionView) CollectionPresent.this.mvpView).fail(str);
            }
        });
    }
}
